package Array;

import java.util.Arrays;

public class lc35 {
    public int searchInsert1(int[] nums, int target) {
        int i = Arrays.binarySearch(nums, target);
        return i >= 0 ? i : ~i;
    }

    public int searchInsert2(int[] nums, int target) {
        return lowerBound(nums, target);
    }

    public int lowerBound(int[] nums, int target) {
        int left = 0;
        int right = nums.length;
        while(left < right){
            int middle = left + (right - left) / 2;
            if(nums[middle] < target){
                left = middle + 1;
            } else {
                right = middle;
            }
        }
        return left;
    }
}
